import { call, put, takeEvery } from 'redux-saga/effects';
import * as actionTypes from '@src/model/actionType';
import * as API from '@src/api/index';
import { get as _get } from 'lodash';
import { timeFormatHm } from '@src/utils/index';

function* fetchTransactionNumByTime(action) {
  const params = _get(action, ['params'], {});
  try {
    const data = yield call(API.getTransactionNumByTime, params);
    const list = _get(data, ['GroupList'], []);
    const len = list.length;
    const GroupList = [];
    for (let i = len - 1; i >= 0; i--) {
      list[i].time = timeFormatHm(list[i].Timestamp);
      GroupList.push(list[i]);
    }
    yield put({
      type: actionTypes.transaction,
      payload: {
        TotalCount: _get(data, ['TotalCount'], 0),
        GroupList,
      },
    });
  } catch (err) {
    console.error(err);
  }
}

const sagas = [takeEvery(actionTypes.fetchTransactionNumByTime, fetchTransactionNumByTime)];

export default sagas;
